/** @file gui_image.cpp
    @brief An image widget */

#include "gui/gui_image.h"

//==============================================================================
// Constructors
//==============================================================================
G3::GUI::IMAGE::IMAGE () {
    Color = COL4F (1, 1, 1, 1);
    FrameVisible = true;
}

//==============================================================================
// Destructors
//==============================================================================

// Just chill?

//==============================================================================
// = operators
//==============================================================================
G3::GUI::IMAGE& G3::GUI::IMAGE::operator= (const IMAGE &aImage) {
    Visible =aImage.Visible;
    Rect = aImage.Rect;
    Children = aImage.Children;

    Color = aImage.Color;
    FrameVisible = aImage.FrameVisible;

    Image = aImage.Image;

    return *this;
}

//==============================================================================
// Creates the group
//==============================================================================
void G3::GUI::IMAGE::Create (const RECTI &aRect, H_TEXTURE aTex) {
    SetImage (aTex);
    SetRect (aRect);
}

//==============================================================================
// Sets group dimensions
//==============================================================================
void G3::GUI::IMAGE::SetRect (const RECTI &aRect) {
    Rect.Left = aRect.Left;
    Rect.Top = aRect.Top;

    if (!Children.empty () && Children [0]) {
        Children [0]->SetPosition (VEC2I (Rect.Left + 7, Rect.Top));

        Rect.Right = G3_MAX (aRect.Right, Children [0]->Rect.Right + 7);
        Rect.Bottom = G3_MAX (aRect.Bottom, Children [0]->Rect.Bottom + 2);
    } else {
        Rect.Right = aRect.Right;
        Rect.Bottom = aRect.Bottom;
    }
}

//==============================================================================
// Sets group label value
//==============================================================================
void G3::GUI::IMAGE::SetImage (H_TEXTURE aTex) {
    Image = aTex;
}

//==============================================================================
// Draws the group
//==============================================================================
void G3::GUI::IMAGE::Draw (float aElapsedTime) {
    if (!Visible)
        return;

    glPushMatrix ();

    glEnable (GL_TEXTURE_2D);

    if (!Image.IsNull()) {
        glColor4f (1, 1, 1, 1);

        G3::TextureManager.Apply (Image);

        glBegin(GL_QUADS);
        glTexCoord2f(0, 0); glVertex2i (Rect.Left, -Rect.Top);
        glTexCoord2f(0, 1); glVertex2i (Rect.Left, -Rect.Bottom);
        glTexCoord2f(1, 1); glVertex2i (Rect.Right, -Rect.Bottom);
        glTexCoord2f(1, 0); glVertex2i (Rect.Right, -Rect.Top);
        glEnd();
    }

    glDisable (GL_TEXTURE_2D);

    if (FrameVisible) {
        // Group frame color
        glColor4f (Color.r, Color.g, Color.b, Color.a);

        glBegin (GL_LINE_STRIP);
        glVertex2i (Rect.Left, -Rect.Top);
        glVertex2i (Rect.Left, -Rect.Bottom);
        glVertex2i (Rect.Right, -Rect.Bottom);
        glVertex2i (Rect.Right, -Rect.Top);
        glVertex2i (Rect.Left, -Rect.Top);
        glEnd ();
    }

    glEnable (GL_TEXTURE_2D);

    glPopMatrix ();
}
